package validation;

import coma2012.Communicator;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import sun.scheduler.io.ScheduleReader;

/**
 * A simple class showing how to read input/output files from hard drive and
 * submit the data to the validator class.
 */
public class Test_1 {

    public static void main(String[] args) {

        /*
         * This tries to submit computed results for the file t1_28. The name of
         * the instance is submitted as third parameter, it is not needed to
         * provide the input itself. Using this method, the results are not only
         * verified, but also added to the ranking on the website. The first two
         * parameter are the number of the company and the corresponding
         * password that you should have received by other means. The last two
         * parameters have to contain the file content as string, the job output
         * is the fourth parameter, the ressource output the last parameter.
         */
        try {
            
            String feedback = Communicator.sendOutputToValidator(15, "DQCabHaJ", "prA", readFile(fileChooser("JOBS-OUTPUT")), readFile(fileChooser("RESOURCES-OUTPUT")));
            System.out.println(feedback);
        } catch (Exception e) {
            // TODO: handle exception
        }

        /*
         * This variant submits the result for an input that is also to be
         * submitted. Note that using this variant, the results are only
         * verified and not used for the ranking. The first two parameters
         * remain the same, the company number and the password. The next two
         * parameters specifiy the instance used to compute the output. The
         * strings again have to contain the file content, first the job input
         * and then the ressource input. The last parameters specify the
         * computed result, the job and ressoruce output.
         */
        
        String origJob = fileChooser("JOBS-ORIGINAL"), origRes = fileChooser("RESOURCES-ORIGINAL"), outJob = fileChooser("JOBS-OUTPUT"), outRes = fileChooser("RESOURCES-OUTPUT");
        try {
            String feedback = Communicator.sendOutputToValidator(15, "DQCabHaJ", readFile(origJob), readFile(origRes), readFile(outJob), readFile(outRes));
            System.out.println(feedback);
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    /**
     * Reads a file from hard drive and returns the content as string.
     *
     * @param fileName the file that is read
     * @return the content of the file as string
     */
    public static String readFile(String fileName) {
        File file = new File(fileName);
        StringBuilder outputStringBuilder = new StringBuilder((int) file.length() * 2);

        try {
            FileInputStream fis = new FileInputStream(fileName);
            BufferedInputStream bis = new BufferedInputStream(fis);

            while (true) {
                int c = bis.read();
                if (c == -1) {
                    break;
                }
                outputStringBuilder.append((char) c);
            }

            bis.close();
            fis.close();
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace(System.err);
        }
        return outputStringBuilder.toString();
    }

    public static String fileChooser(String filetype) {
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Choose a " + filetype + " file...");
        fc.setFileFilter(new FileFilter() {

            public String getDescription() {
                return "CSV-File(*.csv)";
            }

            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".csv")
                        || f.isDirectory();

            }
        });
        int res = fc.showOpenDialog(null);
        if (res == JFileChooser.APPROVE_OPTION) {

            return fc.getSelectedFile().getAbsolutePath();

        }
        return null;
    }
}
